package
{
	import flash.desktop.NativeApplication;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import as3sdk.utils.UtilXMLParse;
	
	/**
	 * ATF资源打包工具 
	 * @author hanxianming
	 * 
	 */	
	public class ATFPackage extends Sprite
	{
		
		private var _fileList:Array;
		private var _urlLoader:URLLoader;
		private var _urlRequest:URLRequest;
		private var _configByteArray:ByteArray;
		private var _urlConfigLoader:URLLoader;
		
		private var loadConfigList:Vector.<FolderNode>;
		
		private var _curFolderNode:FolderNode;
		
		private var _appXML:String = "XML2ObjectTool.xml";
		
		private var _appBat:String = "app.bat";
		
		private var _configCompress:String = "configCompress.xml";
		
		private var _curAppId:String;
		
		private var _configNumber:int;
		
		private var _stringList:String = "";

		public function ATFPackage()
		{
			initizlize();
		}
		
		private function initizlize():void
		{
			
			_urlRequest = new URLRequest();
			
			_urlConfigLoader = new URLLoader();
			_urlConfigLoader.dataFormat = URLLoaderDataFormat.TEXT;
			_urlConfigLoader.addEventListener(Event.COMPLETE, urlConfigLoaderCompleteHandler);
			
			_urlRequest.url = "configCompress.xml";
			//			_urlRequest.url = _appXML;
			_urlConfigLoader.load(_urlRequest);
		}
		
		protected function urlConfigLoaderCompleteHandler(event:Event):void
		{
			loadConfigList = new Vector.<FolderNode>();
			
			var xml:XML = new XML(_urlConfigLoader.data);
			var configObject:Object = xml2Object(xml);
			
			var tempFile:File;
			var folderNode:FolderNode;
			
			for each (var item:Object in configObject) 
			{
				if (item is String)
				{
					continue;
				}
				
				tempFile = File.applicationDirectory;
				tempFile.nativePath = item["folder"];
				
				folderNode = new FolderNode();
				folderNode.copyMoveTarget = item.copyMoveTarget;
				folderNode.folder = item.folder;
				folderNode.exportName = item.exportName;
				folderNode.command = item.command;
				folderNode.commandSourceFile = item.commandSourceFile;
				folderNode.atfCompleteOpen = item.atfCompleteOpen;
				try
				{
					folderNode.fileList = tempFile.getDirectoryListing();
				} 
				catch(error:Error) 
				{
					trace("[error] File.getDirectoryListing() error  " + tempFile.nativePath);
					continue;
				}
				for each (var item1:Object in item) 
				{
					if (item1 is String)
					{
						continue;
					}
					folderNode.compressList.push(item1["configName"]);
				}
				
				loadConfigList.push(folderNode);
			}
			
			loadFolder();			
		}		
		
		private function loadFolder():void
		{
			if (loadConfigList.length <= 0)
			{
				trace("[info] all completed......" + new Date().toString());
				
				NativeApplication.nativeApplication.exit();
				return;
			}
			
			_curFolderNode = loadConfigList.shift();
			
			_fileList = _curFolderNode.fileList;
			
			_configNumber = _fileList.length;
			
			for (var i:int = 0; i < _configNumber; i++) 
			{
				var file:File = _fileList[i];
				
				if (file.extension == "png" || file.extension == "jpg")
				{
					_stringList += _curFolderNode.commandSourceFile + " " + 
						_curFolderNode.command + " " + file.nativePath + 
						" -o " + file.nativePath.replace("." + file.extension, ".atf") + "\n";
				}
			}
			
			if (loadConfigList.length == 0)
			{
				_stringList += "\n\n";
				_stringList += _curFolderNode.atfCompleteOpen;
				
				var fileStream:FileStream;
				try 
				{
					trace("[info] " + " create " + _curFolderNode.exportName + ".....");
					var tempFile:File = File.applicationDirectory;
					tempFile.nativePath = _curFolderNode.exportName;
					fileStream = new FileStream();
					fileStream.open(tempFile, FileMode.WRITE);
					fileStream.writeUTFBytes(_stringList);
					fileStream.close();
					tempFile.openWithDefaultApplication();
					
	//				var nativeProgress:NativeProcess = new NativeProcess();
	//				var nativeProgressStartInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
	//				nativeProgressStartInfo.executable = tempFile;
	//				nativeProgressStartInfo.workingDirectory = File.applicationDirectory;
	//				nativeProgressStartInfo.arguments = new Vector.<String>();
	//				nativeProgress.start(nativeProgressStartInfo);
					
				}catch (e:Error) 
				{
					trace("[error] fileStream error：" + tempFile.nativePath);
				}
			}
			
			fileStream = null;
			
			loadFolder();
		}

		
		public static function xml2Object(xml:XML):Object
		{
			return UtilXMLParse.xml2Object(xml);
		}
	}
}

class FolderNode
{
	public var exportName:String;
	public var folder:String;
	public var compressList:Vector.<String> = new Vector.<String>();
	public var fileList:Array;
	public var copyMoveTarget:String;
	public var sourceDirectory:String;
	public var command:String;
	public var commandSourceFile:String;
	public var atfCompleteOpen:String;
}